package me.zhengjie.WcsReport;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.dto.*;
import me.zhengjie.pojo.entity.WcsAgvId;
import me.zhengjie.pojo.entity.WcsDeliveryMissionHistory;
import me.zhengjie.pojo.entity.WcsThreeFloorOutboundDetailsHistory;
import me.zhengjie.pojo.vo.ReturnVo;
import me.zhengjie.pojo.vo.TestVo;
import me.zhengjie.sqlservice.WcsAgvIdService;
import me.zhengjie.sqlservice.WcsDeliveryMissionHistoryService;
import me.zhengjie.sqlservice.WcsThreeFloorOutboundDetailsHistoryService;
import me.zhengjie.utility.state.Manufacturer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class WcsReportServiceImpl implements WcsReportService{
    private static final Logger wms = LoggerFactory.getLogger("wms");

    private final RestTemplate restTemplate;

    private final WcsAgvIdService wcsAgvIdService;

    private final WcsThreeFloorOutboundDetailsHistoryService wcsThreeFloorOutboundDetailsHistoryService;

    private final WcsDeliveryMissionHistoryService wcsDeliveryMissionHistoryService;

//    private final WcsReportService wcsReportService;


    @Override
    public boolean inboundIsCodeExist(InboundIsCodeExistDto inboundIsCodeExist) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        wms.info("请求参数:{}，结果:{}",inboundIsCodeExist,wcsAgvId);
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/inbound/isCodeExist";
        ReturnVo returnVo = restTemplate.postForObject(url, inboundIsCodeExist, ReturnVo.class);
        wms.info("入库接口请求参数:{},url地址：{}，返回结果:{}",inboundIsCodeExist,url,returnVo);
        Integer code = returnVo.getCode();
        if (200 == code){
            //成功
            boolean data = (boolean) returnVo.getData();
            return data;
        }
        return true;
    }

//    @Override
//    public String inboundStorageApply(InboundStorageApplyDto inboundStorageApplyDto) {
//        wms.info("入库接口请求请求参数：{}",inboundStorageApplyDto);
//        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
//        String ip = wcsAgvId.getIp();
//        String url = ip +"/wms/inbound/storageApply";
//        ReturnVo returnVo = restTemplate.postForObject(url, inboundStorageApplyDto, ReturnVo.class);
//        wms.info("入库接口请求参数:{},url地址：{}，返回结果:{}",inboundStorageApplyDto,url,returnVo);
//        Integer code = returnVo.getCode();
//        if (200 == code){
//            //成功
//            String data = (String) returnVo.getData();
//            return data;
//        }
//        return null;
//    }

    @Override
    public String inboundStorageApply(InboundStorageApplyDto1 inboundStorageApplyDto) {
        wms.info("入库接口请求请求参数：{}",inboundStorageApplyDto);
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/inbound/storageApply";
        ReturnVo returnVo = restTemplate.postForObject(url, inboundStorageApplyDto, ReturnVo.class);
        wms.info("入库接口请求参数:{},url地址：{}，返回结果:{}",inboundStorageApplyDto,url,returnVo);
        Integer code = returnVo.getCode();
        if (200 == code){
            //成功
            String data = (String) returnVo.getData();
            return data;
        }
        return null;
    }

//    @Override
//    public boolean inboundCompleted(InboundCompletedDto inboundCompletedDto) {
//        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WCS.getManufacturer());
//        String ip = wcsAgvId.getIp();
//        String url = ip +"/wms/inbound/completedUpdate";
//        ReturnVo returnVo = restTemplate.postForObject(url, inboundCompletedDto, ReturnVo.class);
//        wms.info("入库接口请求参数:{},url地址：{}，返回结果:{}",inboundCompletedDto,url,returnVo);
//        Integer code = returnVo.getCode();
//        if (200 == code){
//            return true;
//        }
//        return false;
//    }

    @Override
    public boolean pickPlaceReport(takeReportDto takeReportDto) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        wms.info("取放货参数通知WMS：{},IP:{}",takeReportDto,ip);
        String url = ip +"/wms/outbound/pickPlace";
        ReturnVo returnVo = restTemplate.postForObject(url, takeReportDto, ReturnVo.class);
        wms.info("入库接口请求参数:{},url地址：{}，返回结果:{}",takeReportDto,url,returnVo);
        Integer code = returnVo.getCode();
        if (200 == code){
            return true;
        }
        return false;
    }

    @Override
    public ReturnVo circleLibraryObtainPalletCode(String palletCode) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/circleLibrary/palletCode";
        ReturnVo returnVo = restTemplate.postForObject(url, palletCode, ReturnVo.class);
        wms.info("回库接口请求参数:{},url地址：{}，返回结果:{}",palletCode,url,returnVo);
        return returnVo;
    }

    @Override
    public ReturnVo emptyContainer(String station) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/emptyContainer";
        ReturnVo returnVo = restTemplate.postForObject(url, station, ReturnVo.class);
        wms.info("空箱起点申请接口请求参数:{},url地址：{}，返回结果:{}",station,url,returnVo);
        return returnVo;
    }

    @Override
    public ReturnVo middleNestingData(String containerCode) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.middle.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/middle";
        ReturnVo returnVo = restTemplate.postForObject(url, containerCode, ReturnVo.class);
        wms.info("中间表嵌套信息请求参数:{},url地址：{}，返回结果:{}",containerCode,url,returnVo);
        return returnVo;
    }

//    @Override
//    public ReturnVo wmsInventoryInformation(String containerCode) {
//        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
//        String ip = wcsAgvId.getIp();
//        String url = ip +"/api/material/findMaterialByContainerCode";
//        ReturnVo returnVo = restTemplate.postForObject(url, containerCode, ReturnVo.class);
//        wms.info("WMS库存信息接口请求参数:{},url地址：{}，返回结果:{}",containerCode,url,returnVo);
//        return returnVo;
//    }

    @Override
    public TestVo wmsInventoryInformation(String containerCode) {
        wms.info("WMS库存信息接口请求参数:{}",containerCode);
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/api/material/findMaterialByContainerCode";
        // 1. 构造 JSON 请求体
        Map<String, String> body = Collections.singletonMap("containerCode", containerCode);
        // 2. 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);

        TestVo returnVo = restTemplate.postForObject(url, entity, TestVo.class);
        wms.info("WMS库存信息接口请求参数:{},url地址：{}，返回结果:{}",containerCode,url,returnVo);
        return returnVo;
    }

    @Override
    public boolean cancelInboundStorage(CancelInboundStorageDto cancelInboundStorageDto) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/inbound/revokeApply";
        ReturnVo returnVo = restTemplate.postForObject(url, cancelInboundStorageDto, ReturnVo.class);
        wms.info("WMS库存信息接口请求参数:{},url地址：{}，返回结果:{}",cancelInboundStorageDto,url,returnVo);
        if (200 == returnVo.getCode()){
            return true;
        }
        return false;
    }

    @Override
    public boolean storageIsAvailable(StorageIsAvailableDto storageIsAvailableDto) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/api/storeLocal/storageIsAvailable";
        ReturnVo returnVo = restTemplate.postForObject(url, storageIsAvailableDto, ReturnVo.class);
        wms.info("查询库外储位是否能可用请求参数:{},url地址：{}，返回结果:{}",storageIsAvailableDto,url,returnVo);
        if (200 == returnVo.getCode()){
            return true;
        }
        return false;
    }

    @Override
    public boolean wmsThreeFloorDeliveryProductionLine(ThreeFloorDeliveryProductionLineDto threeFloorDeliveryProductionLineDto) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/wms/threeFloor/delivery/productionLine";
        ReturnVo returnVo = restTemplate.postForObject(url, threeFloorDeliveryProductionLineDto, ReturnVo.class);
        wms.info("3楼配送到产线请求参数:{},url地址：{}，返回结果:{}",threeFloorDeliveryProductionLineDto,url,returnVo);
        if (200 == returnVo.getCode()){
            return true;
        }
        return false;
    }

    @Override
    public boolean wmsThreeFloorWorkOrderComplete(String workOrder) {
        WcsAgvId wcsAgvId = wcsAgvIdService.selectName(Manufacturer.WMS.getManufacturer());
        String ip = wcsAgvId.getIp();
        String url = ip +"/api/outBoundOrder/workOrder/complete?workOrder="+workOrder;
        ReturnVo returnVo = restTemplate.getForObject(url, ReturnVo.class,workOrder);
        wms.info("3楼工单完成:{},url地址：{}，返回结果:{}",workOrder,url,returnVo);
        if (200 == returnVo.getCode()){
            return true;
        }
        return false;
    }

    @Override
    public ReturnVo againIssue() {

        return null;
    }

    @Override
    public ReturnVo outboundDeliveryProductionLine(List<String> workOrderList) {
        if (!workOrderList.isEmpty()){
            for (String workOrder : workOrderList) {
                List<WcsThreeFloorOutboundDetailsHistory> wcsThreeFloorOutboundDetailsHistoryList = wcsThreeFloorOutboundDetailsHistoryService.selectWorkOrder(workOrder);
                if (!wcsThreeFloorOutboundDetailsHistoryList.isEmpty()){
                    WcsThreeFloorOutboundDetailsHistory wcsThreeFloorOutboundDetailsHistory = wcsThreeFloorOutboundDetailsHistoryList.get(0);
                    Integer palletCode = wcsThreeFloorOutboundDetailsHistory.getPalletCode();
                    WcsDeliveryMissionHistory wcsDeliveryMissionHistory = wcsDeliveryMissionHistoryService.selectContainerCode(palletCode);
                    if (wcsDeliveryMissionHistory !=null){
                        List<String> collect = wcsThreeFloorOutboundDetailsHistoryList.stream().map(WcsThreeFloorOutboundDetailsHistory::getWorkOrder).collect(Collectors.toList());
                        ThreeFloorDeliveryProductionLineDto threeFloorDeliveryProductionLineDto = new ThreeFloorDeliveryProductionLineDto();
                        threeFloorDeliveryProductionLineDto.setWorkOrder(workOrder);
                        threeFloorDeliveryProductionLineDto.setStorageCode(wcsDeliveryMissionHistory.getDestination());
                        threeFloorDeliveryProductionLineDto.setContainerCodeList(collect);
                        threeFloorDeliveryProductionLineDto.setSource("R3FchuA01011");
                        threeFloorDeliveryProductionLineDto.setType(2);
                        boolean b2 = wmsThreeFloorDeliveryProductionLine(threeFloorDeliveryProductionLineDto);
                        if (!b2) {
                            throw new RuntimeException("配送到线边WMS报错");
                        }
                    }
                }
            }
            return ReturnVo.OK();
        }
        return null;
    }


}
